﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataTest
{
    public static class Class1
    {
        #region 检查长度
        /// <summary>
        /// 检查字符串长度
        /// </summary>
        /// <param name="source"></param>
        /// <param name="minlength">最小长度</param>
        /// <param name="maxlenth">最大长度</param>
        /// <param name="msg">消息</param>
        /// <param name="allownull">是否允许字符串为空，如果为空，默认0长度</param>
        /// <returns></returns>
        public static bool CheckLength(this String source, int minlength, int maxlenth, ref string msg, bool allownull = false)
        {
            msg = string.Empty;

            if (source == null)
            {
                if (!allownull)
                {
                    msg = "不允许为空。";
                    return false;
                }
                source = string.Empty;
            }
            if (source.Length < minlength)
            {
                msg = string.Format("长度小于{0}", minlength);
                return false;
            }
            if (source.Length > maxlenth)
            {
                msg = string.Format("长度超过{0}", maxlenth);
                return false;
            }
            return true;
        }

        #endregion
    }
}
